package com.jiaai.web.community;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.community.api.InCommunityApi;
import com.jiaai.community.model.InCommentaries;
import com.jiaai.community.model.InCommunity;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResult;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/app/community")
public class CommunityController {
    @Reference
    private InCommunityApi inCommunityApi;
    /**
    * 获取社区
    * */
    @GetMapping("/list/{curr}")
    public Object view(@PathVariable("curr") Integer curr){
        AccessUser user = RequestUtils.getUser();
        return RestResultGenerator.genSuccessResult(inCommunityApi.view(curr,user.getUserId()));
    }
    /**
    * 发布社区
    * */
    @PostMapping("/posted")
    @TokenIntercept(checkVisitor = true)
    public RestResult<Boolean> posted(@RequestBody InCommunity inCommunity){
        AccessUser user = RequestUtils.getUser();
        inCommunity.setMemberId(user.getUserId());
        return RestResultGenerator.genSuccessResult(inCommunityApi.posted(inCommunity));
    }
    /**
     * 社区详情
     * 传入社区id
     * */
    @GetMapping("/detail/{comm}")
    @TokenIntercept
    public Object comm(@PathVariable("comm") int comm){
        AccessUser user = RequestUtils.getUser();
       return RestResultGenerator.genSuccessResult(  inCommunityApi.comm(comm,user.getUserId()));
    }
    /**
     * 增加一条评论，数量+1
     * 传入整个对象
     * */
    @PostMapping("/commAdd")
    @TokenIntercept(checkVisitor = true)
    public RestResult<Boolean> commAdd(@RequestBody InCommentaries inCommentaries){
        AccessUser user = RequestUtils.getUser();
        inCommentaries.setMember(user.getUserId());
        return RestResultGenerator.genSuccessResult(inCommunityApi.commAdd(inCommentaries));
    }
    /**
     * 搜索当前社区评论
     * 需要传入社区id
     * */
    @RequestMapping("/seachCommes/{i}")
    public Object seachCommes(@PathVariable("i") int i) {
        return RestResultGenerator.genSuccessResult(inCommunityApi.seachCommes(i));
    }




    /*
    =========================================
     */


    /**
     * 查询点赞
     * 传入会员id
     * 无用
     * */
    @GetMapping("/zan/{i}")
    public Object zan(@PathVariable("i") String i){
        return inCommunityApi.zan(i);
    }
    /**
     * 点赞+1
     * 插入点赞数据
     * */
    @GetMapping("/zanAdd/{memberId}/{id}")
    public boolean zanAdd(@PathVariable("memberId") String memberId,@PathVariable("id") int id){
        return inCommunityApi.zanAdd(memberId,id);
    }
    /**
     * 点赞-1
     * 删除相关信息，传入点赞表自增ID
     * */
    @GetMapping("/zanSub/{memberId}/{id}")
    public boolean zanSub(@PathVariable("memberId") String memberId,@PathVariable("id") int id){
        return inCommunityApi.zanSub(memberId,id);
    }
    /**
     * 删除一条评论，数量-1
     * 传入需要删除的评论自增id
     * */
    @PostMapping("/commSub/{i}")
    public boolean commSub(@PathVariable("i") int i){
        return  inCommunityApi.commSub(i, 1);
    }
    /**
     * 判断是否是主播
     * 1，是主播；0，不是主播
     * 需要传入会员id
     * */
    @GetMapping("/yn/{s}")
    public int yn(@PathVariable("s") String s){
        return inCommunityApi.yn(s);
    }

    /**
     * 查询点赞头像
     * 传入当前社区id
     * */
    @RequestMapping("/seachFAvatar/{i}")
    public Object seachAvatar(@PathVariable("i") int i){
        return inCommunityApi.seachAvatar(i);
    }
}
